package com.sgb.kjwl.view.ui.activity.manage;

import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestManager;
import com.bumptech.glide.request.RequestOptions;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityWebsiteQyjjBinding;
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageComponent;
import com.sgb.kjwl.di.manage.EnterpriseManageModule;
import com.sgb.link.entity.PickFilterEntity;
import com.sgb.kjwl.model.entity.manage.WebsiteInfoEntity;
import com.sgb.kjwl.view.ui.adapter.manage.AddViewAdapter;
import com.sgb.kjwl.view.widget.manage.WebsiteUtils;
import com.sgb.kjwl.view.widget.manage.MImgGetter;
import com.sgb.kjwl.viewmodel.manage.IntroductionForWebsiteModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.util.ClickUtils;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;


/**
 * @author 张驰
 * @date 2021/5/12 15:03
 * @Description 企业网店发布模块中企业简介数据展示
 * @name com.sgb.kjwl.view.ui.activity.manage
 */
public class WebsiteQiYeXiXinActivity extends AppActivity {

    @Inject
    IntroductionForWebsiteModel viewModel;
    private ActivityWebsiteQyjjBinding binding;
    private String noData = "--";
    private String noDataY = "--";
    //private TagAdapter mAdapter;
    private RequestManager requestManager;
    private AddViewAdapter mAddViewAdapter;
    private WebsiteInfoEntity mEntity;


    public static void start(Context context, int qiyexinxi) {
        Intent intent = new Intent(context, WebsiteQiYeXiXinActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerEnterpriseManageComponent.builder().appComponent(appComponent).
                enterpriseManageModule(new EnterpriseManageModule(this)).
                build().inject(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_website_qyjj);
        requestManager = Glide.with(this);
        binding.include.titleView.setText("信息简介");
        binding.tvUpdate.setOnClickListener(this);
        binding.include.ivBack.setOnClickListener(this);
        binding.rlVideoView.setOnClickListener(this);
        initRefreshView();
        viewModel.getWebsiteInfoEntityMutableLiveData().observe(this, new Observer<WebsiteInfoEntity>() {
            @Override
            public void onChanged(WebsiteInfoEntity websiteInfoEntity) {
                mEntity = websiteInfoEntity;
                binding.setViewModel(websiteInfoEntity);
                showWebsiteInfo(websiteInfoEntity);
            }
        });
        mAddViewAdapter = new AddViewAdapter(this);
        mAddViewAdapter.setEnableEdit(false);
        binding.addView.setHideAdd();
        binding.addView.setAdapter(mAddViewAdapter);
    }

    private void initRefreshView() {
        binding.swipeRefresh.setEnableLoadMore(false);
        // 刷新
        binding.swipeRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                onResume();
                binding.swipeRefresh.finishRefresh(500);
            }
        });
        //mAdapter = new TagAdapter(this, R.layout.item_tag_website_info, new ArrayList<>());
//        mAdapter.setOnItemClickListener(new CommonBaseAdapter.OnItemClickListener() {
//            @Override
//            public void onItemClick(int position, View itemView) {
//                if (TextUtils.isEmpty(mAdapter.getItem(position))) {
//                    if (DialogHelper.checkLogin()) {
//                        DialogHelper.showDialog(getActivity(),
//                                "经营业务",
//                                viewModel.getWebsiteInfoEntityMutableLiveData().getValue().getServiceTypeName().replace(",", "、"),
//                                new DialogFunction() {
//                                    @Override
//                                    public void normalRoundDialog() {
//                                    }
//                                });
//                    } else {
//                        DialogHelper.showLoginDialog(getActivity(), EVENT_INTELLPROP_DETAIL_JUMP);
//                    }
//                }
//            }
//        });
        //binding.recyclerViewTag.setAdapter(mAdapter);
    }

    @Override
    protected void init() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        //网店编号
        String companyNo = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.COMPANY_NO, "");
        viewModel.getWebsiteInfo(companyNo, 1);  // 目前固定为企业
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_update:
                if (!ClickUtils.isDoubleClick()) {
                    WebsiteUpDataQiYeXiXinActivity.start(this);
                }
                break;
            case R.id.rl_video_view:
                WebsiteUtils.getInstance().openVideo(this, binding.getViewModel().getVideo(), binding.getViewModel().getVideoUrl(), binding.getViewModel().getCoverUrl());
                break;
        }
    }

    public void showWebsiteInfo(WebsiteInfoEntity entity) {
        settitleView(entity);
        if (!TextUtils.isEmpty(entity.getNewMainBusinessName())){
            binding.comServiceTypeDetail.setText(entity.getNewMainBusinessName());
        }else {
            binding.comServiceTypeDetail.setText(noData);
        }
        if (TextUtils.isEmpty(entity.getIndustryCategoryOneName())) {
            binding.tvIndustryCategory.setText(noData);
        } else {
            if (TextUtils.isEmpty(entity.getIndustryCategoryTwoName())) {
                binding.tvIndustryCategory.setText(entity.getIndustryCategoryOneName());
            } else {
                binding.tvIndustryCategory.setText(entity.getIndustryCategoryOneName() + "/" + entity.getIndustryCategoryTwoName());
            }
        }
        if (TextUtils.isEmpty(entity.getCompanyQualityName())) {
            binding.tvCompanyNature.setText(noData);
        } else {
            binding.tvCompanyNature.setText(entity.getCompanyQualityName());
        }
        if (TextUtils.isEmpty(entity.getFixedNumber())) {
            binding.tvFixedNumber.setText(noData);
        } else {
            binding.tvFixedNumber.setText(entity.getFixedNumber());
        }
        if (TextUtils.isEmpty(entity.getConcat())) {
            binding.tvConcat.setText(noData);
        } else {
            binding.tvConcat.setText(entity.getConcat());
        }
        if (TextUtils.isEmpty(entity.getPhone())) {
            binding.tvPhone.setText(noData);
        } else {
            binding.tvPhone.setText(entity.getPhone());
        }
        if (TextUtils.isEmpty(entity.getRecordUnitName())) {
            binding.tvRecordUnitName.setText(noData);
        } else {
            binding.tvRecordUnitName.setText(entity.getRecordUnitName());
        }
        if (TextUtils.isEmpty(entity.getMapAddress())) {
            binding.tvAddress.setText(noData);
        } else {
            binding.tvAddress.setText(entity.getMapAddress());
        }
        if (TextUtils.isEmpty(entity.getServiceAreaNames())) {//服务范围
            binding.tvServiceArea.setText(noData);
        } else {
            binding.tvServiceArea.setText(entity.getServiceAreaNames());
        }
        if (TextUtils.isEmpty(entity.getBusinessInfo().getBusinessModel())){
            binding.jingyingmode.setText(noData);
        }else {
            binding.jingyingmode.setText(entity.getBusinessInfo().getBusinessModel());
        }
//        if (TextUtils.isEmpty(entity.getMainBusiness())) {
//            binding.tvMainBusiness.setText(noData);
//        } else {
//            binding.tvMainBusiness.setText(entity.getMainBusiness());
//        }

//        if (TextUtils.isEmpty(entity.getStaffNumber())) {
//            binding.tvWorkersNum.setText(noDataY);
//        } else {
//            binding.tvWorkersNum.setText(entity.getStaffNumber());
//        }
//        if (TextUtils.isEmpty(entity.getDeveloperNumber())) {
//            binding.tvDeveloperNumber.setText(noDataY);
//        } else {
//            binding.tvDeveloperNumber.setText(entity.getDeveloperNumber());
//        }
//        if (TextUtils.isEmpty(entity.getMonthProduction())) {
//            binding.tvMonthProduction.setText(noDataY);
//        } else {
//            binding.tvMonthProduction.setText(entity.getMonthProduction() + entity.getUnitName());
//        }
//        if (TextUtils.isEmpty(entity.getAnnualIncome())) {
//            binding.tvAnnualIncome.setText(noDataY);
//        } else {
//            binding.tvAnnualIncome.setText(entity.getAnnualIncome());
//        }
//        if (TextUtils.isEmpty(entity.getFactoryArea())) {
//            binding.tvFactoryArea.setText(noDataY);
//        } else {
//            binding.tvFactoryArea.setText(entity.getFactoryArea() + "平方米");
//        }
//        if (TextUtils.isEmpty(entity.getIsProcessing())) {
//            binding.tvIsProcessing.setText(noDataY);
//        } else {
//            if (TextUtils.equals(entity.getIsProcessing(), "0")) {
//                binding.tvIsProcessing.setText("否");
//            } else {
//                binding.tvIsProcessing.setText("是");
//            }
//        }
//        if (TextUtils.isEmpty(entity.getQualityControl())) {
//            binding.tvQualityControl.setText(noDataY);
//        } else {
//            if (entity.getQualityControl().equals("0")) {
//                binding.tvQualityControl.setText("内部");
//            } else {
//                binding.tvQualityControl.setText("第三方");
//            }
//        }
//        if (TextUtils.isEmpty(entity.getBusinessModel())) {
//            binding.tvBusinessModel.setText(noData);
//        } else {
//            binding.tvBusinessModel.setText(entity.getBusinessModel());
//        }
//        if (TextUtils.isEmpty(entity.getManageAuth())) {
//            binding.tvManageAuth.setText(noData);
//        } else {
//            binding.tvManageAuth.setText(entity.getManageAuth());
//        }
//        if (TextUtils.isEmpty(entity.getMainCustomers())) {
//            binding.tvMainCustomers.setText(noData);
//        } else {
//            binding.tvMainCustomers.setText(entity.getMainCustomers());
//        }
//        if (TextUtils.isEmpty(entity.getMajorBrand())) {
//            binding.tvMajorBrand.setText(noData);
//        } else {
//            binding.tvMajorBrand.setText(entity.getMajorBrand());
//        }
//        if (TextUtils.isEmpty(entity.getMarketZone())) {
//            binding.tvArea.setText(noData);
//        } else {
//            List<AddEntity> filterEntityList = new Gson().fromJson(entity.getMarketZone(), new TypeToken<List<AddEntity>>() {
//            }.getType());
//            if (filterEntityList.size() > 0) {
//                StringBuilder builder = new StringBuilder();
//                for (AddEntity areaEntity : filterEntityList) {
//                    builder.append(areaEntity.getName()).append(",");
//                }
//                binding.tvArea.setText(builder);
//            } else {
//                binding.tvArea.setText(noData);
//            }
//        }
        if (TextUtils.isEmpty(entity.getIntroduction())) {
            binding.tvIntroduction.setText(noData);
            binding.linIntroduction.setVisibility(View.GONE);
        } else {
            binding.linIntroduction.setVisibility(View.VISIBLE);
            binding.tvIntroduction.setText(Html.fromHtml(entity.getIntroduction(), new MImgGetter(getActivity(), binding.tvIntroduction), null));
        }
        if (entity.getBusinessInfo() != null){ //如果有业务类型
            changeViewTip(entity.getBusinessInfo());
            renderView(entity.getBusinessInfo(), entity.getBusinessInfo().getType());
            binding.llBusinessMsg.setVisibility(View.VISIBLE);
            binding.llContent.setVisibility(View.VISIBLE);
            binding.setBusinessType(entity.getBusinessInfo().getType());
        }
    }

    private List<PickFilterEntity> areas = new ArrayList<>();

    private void renderView(WebsiteInfoEntity.BusinessInfoEntity entity, int type) {
        if (TextUtils.isEmpty(entity.typeName)) {//业务信息
            binding.tvBusiness.setText(noData);
        } else {
            binding.tvBusiness.setText(entity.typeName);
        }
        String peopleNum = "";
        String annualIncome = "";
        String scope = "";
        String describe = "";
        areas.clear();
        if (type == 0) { //生产型
            peopleNum = entity.staffNumber;
            annualIncome = entity.annualIncome;
            if (TextUtils.isEmpty(entity.dayProduction)) {//日产量
                binding.tvDayOutput.setText(noData);
            } else {
                binding.tvDayOutput.setText(entity.dayProduction + (TextUtils.isEmpty(entity.unit) ? "吨" :entity.unit));
            }
            if (entity.productionRate == 0.00) {//制成率
                binding.tvProductionRate.setText(noData);
            } else {
                binding.tvProductionRate.setText(entity.getRate() + "%");
            }
            if (TextUtils.isEmpty(entity.factoryArea)) {//场地面积
                binding.tvProductionPlaceArea.setText(noData);
            } else {
                binding.tvProductionPlaceArea.setText(entity.factoryArea + "平方米");
            }
            scope = entity.productionScope;
            if (TextUtils.isEmpty(entity.getNames(entity.productionArea))) {//生产区域
                binding.tvProductArea.setText(noData);
            } else {
                binding.tvProductArea.setText(entity.getNames(entity.productionArea));
            }
            describe = entity.majorBrand;
            areas.addAll(entity.mainMarket == null ? new ArrayList<>() : entity.mainMarket);
        } else if (type == 1) {//研发型
            peopleNum = entity.researchNum;
            annualIncome = entity.annualIncome;
            scope = entity.researchRange;
            describe = entity.researchProduct;
            if (TextUtils.isEmpty(entity.customerType)) {//客户类型
                binding.tvServiceTypeContent.setText(noData);
            } else {
                binding.tvServiceTypeContent.setText(entity.customerType);
            }
            areas.addAll(entity.serviceArea == null ? new ArrayList<>() : entity.serviceArea);
        } else if (type == 2) {//贸易型
            peopleNum = entity.staffNumber;
            annualIncome = entity.annualIncome;
            if (TextUtils.isEmpty(entity.factoryArea)) {//贸易场地面积
                binding.tvTradePlaceArea.setText(noData);
            } else {
                binding.tvTradePlaceArea.setText(entity.factoryArea + "平方米");
            }
            if (TextUtils.isEmpty(entity.tradeAmount)) {//贸易量
                binding.tvTradeVolume.setText(noData);
            } else {
                binding.tvTradeVolume.setText(entity.tradeAmount + "次");
            }
            scope = entity.tradeScope;
            describe = entity.tradeBrand;
            areas.addAll(entity.tradeRegion == null ? new ArrayList<>() : entity.tradeRegion);
        } else if (type == 3) {//服务型
            peopleNum = entity.staffNumber;
            annualIncome = entity.annualIncome;
            scope = entity.serviceScope;
            describe = entity.serviceCustomer;
            if (TextUtils.isEmpty(entity.serviceType)) {//服务类型
                binding.tvServiceTypeContent.setText(noData);
            } else {
                binding.tvServiceTypeContent.setText(entity.serviceType);
            }
            areas.addAll(entity.serviceArea == null ? new ArrayList<>() : entity.serviceArea);
        } else if (type == 4) {
            peopleNum = entity.staffNumber;
            annualIncome = entity.annualIncome;
            if (TextUtils.isEmpty(entity.wholesaleOrder)) {//批发订单
                binding.tvWholesaleOrder.setText(noData);
            } else {
                binding.tvWholesaleOrder.setText(entity.wholesaleOrder + "单");
            }
            if (TextUtils.isEmpty(entity.purchaseOrder)) {//采购订单
                binding.tvWholesalePurchaseOrder.setText(noData);
            } else {
                binding.tvWholesalePurchaseOrder.setText(entity.purchaseOrder + "单");
            }
            if (TextUtils.isEmpty(entity.factoryArea)) {//场地面积
                binding.tvWholesalePlaceArea.setText(noData);
            } else {
                binding.tvWholesalePlaceArea.setText(entity.factoryArea + "平方米");
            }
            scope = entity.wholesaleScope;
            describe = entity.majorBrand;
            areas.addAll(entity.wholesaleArea == null ? new ArrayList<>() : entity.wholesaleArea);

        } else if (entity.getType() == 5) {
            peopleNum = entity.staffNumber;
            annualIncome = entity.annualIncome;
            if (TextUtils.isEmpty(entity.retailOrder)) {//零售订单
                binding.tvRetailOrder.setText(noData);
            } else {
                binding.tvRetailOrder.setText(entity.retailOrder + "单");
            }
            if (TextUtils.isEmpty(entity.retailOrder)) {//零售门店
                binding.tvRetailStore.setText(noData);
            } else {
                binding.tvRetailStore.setText(entity.retailStores + "家");
            }

            if (TextUtils.isEmpty(entity.monthTurnover)) {//月营业额
                binding.tvRetailBusinessVolumeMonth.setText(noData);
            } else {
                binding.tvRetailBusinessVolumeMonth.setText(entity.monthTurnover + "万元");
            }
            scope = entity.retailScope;
            describe = entity.retailBrand;
            areas.addAll(entity.retailArea == null ? new ArrayList<>() : entity.retailArea);
        }
        if (TextUtils.isEmpty(peopleNum)) {//员工人数
            binding.tvStaffNum.setText(noData);
        } else {
            binding.tvStaffNum.setText(peopleNum + "人");
        }
        if (TextUtils.isEmpty(annualIncome)) {//年营业额
            binding.tvBusinessVolume.setText(noData);
        } else {
            binding.tvBusinessVolume.setText(annualIncome + "万元");
        }
        if (TextUtils.isEmpty(scope)) {//范围
            binding.tvServiceRangeContent.setText(noData);
        } else {
            binding.tvServiceRangeContent.setText(scope);
        }
        if (TextUtils.isEmpty(describe)) {//描述
            binding.tvServiceCustomerContent.setText(noData);
        } else {
            binding.tvServiceCustomerContent.setText(describe);
        }
        if (TextUtils.isEmpty(entity.getNames(areas))) {//地区
            binding.tvAreaContent.setText(noData);
        } else {
            binding.tvAreaContent.setText(entity.getNames(areas));
        }
        if (entity.customFields != null) {
            binding.addView.setData(entity.customFields);
        } else {
            binding.addView.setData(new ArrayList<>());
        }
    }


    //根据业务类型动态切换提示
    private void changeViewTip(WebsiteInfoEntity.BusinessInfoEntity entity) {
        switch (entity.getType()){
            case 3://服务型
                binding.tvStaffTitle.setText("员工人数");
                binding.tvServiceRange.setText("服务范围");
                binding.tvServiceCustomer.setText("服务客户");
                binding.llServiceType.setVisibility(View.VISIBLE);
                binding.tvServiceType.setText("服务类型");
                binding.llProductArea.setVisibility(View.GONE);
                binding.tvArea.setText("服务地区");
                break;
            case 4://批发型
                binding.tvStaffTitle.setText("员工人数");
                binding.tvServiceRange.setText("批发范围");
                binding.tvServiceCustomer.setText("主营品牌");
                binding.llServiceType.setVisibility(View.GONE);
                binding.llProductArea.setVisibility(View.GONE);
                binding.tvArea.setText("批发地区");

                break;
            case 5://零售型
                binding.tvStaffTitle.setText("员工人数");
                binding.tvServiceRange.setText("零售范围");
                binding.tvServiceCustomer.setText("零售品牌");
                binding.llServiceType.setVisibility(View.GONE);
                binding.llProductArea.setVisibility(View.GONE);
                binding.tvArea.setText("零售地区");

                break;
            case 0://生产型
                binding.tvStaffTitle.setText("员工人数");
                binding.tvServiceRange.setText("生产范围");
                binding.tvServiceCustomer.setText("主营品牌");
                binding.llServiceType.setVisibility(View.GONE);
                binding.llProductArea.setVisibility(View.VISIBLE);
                binding.tvArea.setText("主营市场");
                break;
            case 1://研发型
                binding.tvStaffTitle.setText("研发人数");
                binding.tvServiceRange.setText("研发范围");
                binding.tvServiceCustomer.setText("研发产品");
                binding.llServiceType.setVisibility(View.VISIBLE);
                binding.tvServiceType.setText("客户类型");
                binding.llProductArea.setVisibility(View.GONE);
                binding.tvArea.setText("服务地区");
                break;
            case 2://贸易型
                binding.tvStaffTitle.setText("员工人数");
                binding.tvServiceRange.setText("贸易范围");
                binding.tvServiceCustomer.setText("贸易品牌");
                binding.llServiceType.setVisibility(View.GONE);
                binding.llProductArea.setVisibility(View.GONE);
                binding.tvArea.setText("主营市场");
                break;
        }


    }
    private void settitleView(WebsiteInfoEntity entity) {
        if (!TextUtils.isEmpty(entity.getLogo())) {
            RequestOptions options = new RequestOptions()
                    .placeholder(R.mipmap.image_website_dfg)//图片加载出来前，显示的图片
                    .error(R.mipmap.image_website_dfg);//图片加载失败后，显示的图片
            requestManager.load(entity.getLogo()).apply(options).into(binding.ivShopAvatar);
        }
        if (!TextUtils.isEmpty(entity.getServiceTypeName())) {
            List<String> serviceNameList = new ArrayList<>();
            serviceNameList.addAll(entity.getServiceTypeList(5));
            //mAdapter.setData(serviceNameList);
        }
//        binding.tvMainBusiness.setText(entity.getServiceTypeName());
    }
}
